package com.example.shellcollector;

import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;
import androidx.transition.TransitionInflater;

import com.example.shellcollector.Entity.InstalledAppUsage;
import com.example.shellcollector.database.InstalledAppUsageDao;
import com.example.shellcollector.database.ShellCollectorDatabase;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.MPPointF;
import com.google.android.material.card.MaterialCardView;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;

public class DashboardFragment extends Fragment {
    static final String TAG = "DashboardFragmentTag";
    InstalledAppUsageDao installedAppUsageDao;
    List<InstalledAppUsage> installedAppUsageList ;
    MaterialCardView cardViewApp;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //cardViewApp = (MaterialCardView) getView().findViewById(R.id.apptime);
        appUsageIn();
        TransitionInflater inflater = TransitionInflater.from(requireContext());
        setEnterTransition(inflater.inflateTransition(R.transition.fade));
        setExitTransition(inflater.inflateTransition(R.transition.fade));
    }


    private ArrayList<PieEntry> datavalue(){
        ArrayList<PieEntry> dataVals = new ArrayList<PieEntry>();
        for(int i = 0;i<7&&i<installedAppUsageList.size();i++){
            dataVals.add(new PieEntry(installedAppUsageList.get(i).getTotalRunningTime()/(1000*60),installedAppUsageList.get(i).getAppName()+""));
        }
        int sum = 0;
        for(int i = 7;i<installedAppUsageList.size();i++){
            sum+=installedAppUsageList.get(i).getTotalRunningTime()/(1000*60);
        }
        dataVals.add(new PieEntry(sum,"其他"));
        return dataVals;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_dashborad, container, false);
        cardViewApp = (MaterialCardView) v.findViewById(R.id.apptime);
        PieChart pieChart = v.findViewById(R.id.piechart);
        installedAppUsageDao = ShellCollectorDatabase.getDatabase(getContext()).getInstalledAppUsageDao();
        installedAppUsageList = installedAppUsageDao.getAllOrderByTotalRunningTime(false);
        //int[] colorClassArray = new int[]{Color.rgb(192,255,140),Color.rgb(139,234,254),Color.rgb(244,208,140),Color.rgb(255,247,140),Color.rgb(232,76,61),Color.rgb(202,133,245),Color.rgb(246,150,211),Color.rgb(246,140,142)};
        ArrayList<Integer> colors = new ArrayList<>();
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);
        PieDataSet pieDataSet = new PieDataSet(datavalue(),"");
        pieDataSet.setSliceSpace(3f);
        pieDataSet.setColors(colors);
        pieDataSet.setDrawValues(false);

        pieChart.setDrawSliceText(false);
        pieChart.highlightValues(null);
        pieChart.setDrawEntryLabels(true);
        pieChart.setCenterText(generateCenterSpannableText());
        pieChart.getDescription().setEnabled(false);
        pieChart.animateY(1400, Easing.EaseInOutQuad);
        pieChart.setHoleRadius(58f);
        pieChart.setTransparentCircleRadius(61f);

        PieData pieData = new PieData(pieDataSet);
        pieChart.setData(pieData);
        pieChart.setExtraOffsets(8, 0, 8, 0);
        pieChart.setUsePercentValues(true);

        Legend l = pieChart.getLegend();
        l.setEnabled(false);
        appUsageIn();
        return v;
    }
    private SpannableString generateCenterSpannableText() {

        SpannableString s = new SpannableString("Shell Collector\ndeveloped by Lemon&Snek&Pan");
        s.setSpan(new RelativeSizeSpan(1.7f), 0, 15, 0);
        s.setSpan(new StyleSpan(Typeface.NORMAL), 15, s.length() - 15, 0);
        s.setSpan(new ForegroundColorSpan(Color.GRAY), 15, s.length() - 15, 0);
        s.setSpan(new RelativeSizeSpan(.8f), 15, s.length() - 15, 0);
        s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 15, s.length(), 0);
        s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 15, s.length(), 0);
        return s;
    }

    private void appUsageIn() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(cardViewApp, "translationY", -500, 0);
        animator.setDuration(300)
                .setInterpolator(new FastOutSlowInInterpolator());
        animator.start();
    }
}